package com.curtis.playwings.surface;

import android.content.*;
import android.graphics.*;
import android.util.*;
import android.view.*;

import com.curtis.playwings.*;

//SHS
public class GraphicsView extends SurfaceView implements SurfaceHolder.Callback
{
     private static final String TAG = "TAG";
     private Thread thread;
     private SurfaceHolder holder;
     private int Pos = 0;
     private boolean isLoop;
     private Bitmap XImage, XBackImage;
 
     public GraphicsView(Context context, AttributeSet attrs) {
         super(context, attrs);
         holder = getHolder();
         holder.addCallback(this);
 
     }
 
     @Override
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 
     }
 
     @Override
     public void surfaceCreated(SurfaceHolder holder) {
         isLoop = true;
         createThread();
 
         XImage = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_front);
         XBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.background);
 
         XImage = Bitmap.createScaledBitmap(XImage, SplashActivity.Xsize, SplashActivity.Ysize, true);
         XBackImage = Bitmap.createScaledBitmap(XBackImage, SplashActivity.Xsize, SplashActivity.Ysize, true);
     }
 
     @Override
     public void surfaceDestroyed(SurfaceHolder holder) {
 
         try {
        	 isLoop = false;
             thread.join();
         } catch (Exception e) {
             e.getStackTrace();
         }
     }
 
     private void createThread() {
         thread = new Thread(new Runnable() {
 
             @Override
             public void run() {
                 while (isLoop) {
                     Canvas mCanvas = null;
                     try {
                         mCanvas = holder.lockCanvas();
                         Log.d(TAG, "" + Pos);
                         synchronized (holder) {
 
                             mCanvas.drawBitmap(XBackImage, 0, 0, null);
                             mCanvas.drawBitmap(XImage, Pos, 0, null);
                             mCanvas.drawBitmap(XImage, Pos - SplashActivity.Xsize, 0, null);
                             mCanvas.drawBitmap(XImage, Pos - SplashActivity.Xsize * 2, 0, null);
 
                             Pos += 1;
                             if (Pos >= SplashActivity.Xsize * 2) {
                                 Pos = 0;
                             }
                         }
                     } finally {
                         if (mCanvas != null) {
                             holder.unlockCanvasAndPost(mCanvas);
                         }
                     }
                 }
             }
         });
         thread.start();
     }

}
